package com.fudan.run.action;

import com.fudan.cfg.RunConfiguration;
import com.fudan.cfg.base.ActionDef;
import com.fudan.run.VariableRddMap;
import com.fudan.run.ctx.ActionContext;

public abstract class ActionHandler<T extends ActionDef>{

	public static ActionHandler<? super ActionDef> build(ActionDef actionNode,RunConfiguration runConfiguration) {
		String type = actionNode.getType();
		ActionContext actionContext = runConfiguration.getActionContext();
		Class<? extends ActionHandler<? extends ActionDef>> handler = actionContext.getHandler(type);
		if(handler != null) {
			try {
				return (ActionHandler<? super ActionDef>) handler.newInstance();
			} catch (InstantiationException | IllegalAccessException e) {
				throw new RuntimeException(String.format("构建action的处理器出错，type:%s", type),e);
			}
		}
//		if("foreach".equals(type)) {
//			return new ForeachAction();
//		}

		throw new RuntimeException(String.format("没有找到action的处理器，type:%s", type));
	}

	public abstract void action(T actionNode,VariableRddMap variableRddMap,RunConfiguration runConfiguration);
}
